package com.purchase.ml.resource.dealer;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.purchase.ml.annotations.OperLog;
import com.purchase.ml.bean.base.ApiRestResult;
import com.purchase.ml.bean.req.dealer.*;
import com.purchase.ml.bean.req.mail.DealerStateEditDto;
import com.purchase.ml.bean.res.dealer.SysDealerRes;
import com.purchase.ml.bean.sdk.IpayNewDto;
import com.purchase.ml.consts.Constants;
import com.purchase.ml.consts.PermissionsConst;
import com.purchase.ml.entity.acc.SysUser;
import com.purchase.ml.entity.dealer.SysDealer;
import com.purchase.ml.mapper.dealer.SysDealerMapper;
import com.purchase.ml.security.JwtUtil;
import com.purchase.ml.service.dealer.SysAddrService;
import com.purchase.ml.service.dealer.SysDealerService;
import com.purchase.ml.utils.HttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.annotations.Ignore;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

/**
 * @author :zxm
 * @date :2021/2/25 10:41
 * @description :some description
 **/
@RestController
@RequestMapping("/dealer")
@Slf4j
@Api(tags = "经销商")
public class DealerResource {

    @Autowired
    private SysDealerService sysDealerService;
    @Autowired
    private SysAddrService sysAddrService;

    @PostMapping("/registpay")
    @ApiOperation("经销商请求支付年费：接收到数据提交到三方：跟订单提交一样")
    public ApiRestResult registPay(@Valid @RequestBody SysDealerRegisterPayDto dto) throws NoSuchAlgorithmException {
        //返回数据前端表单提交
        return ApiRestResult.ok(sysDealerService.registerPay(dto));
    }

    @PostMapping("/applay")
//    @ApiOperation("创建经销商户")
    public ApiRestResult applyDealer(@Valid @RequestBody SysDealerNewDto dto){
        dto.setNewType(Constants.DEALER_STATE_REQ);  //注册
        log.info("sssss");
        if(dto.getRegistCode() == null ){
            return ApiRestResult.err("Regiscode Can Not Null","4002");
        }
        ApiRestResult result = sysDealerService.createDealer(dto);
        return result;
    }

    @PostMapping("/new")
    @ApiOperation("创建经销商户")
    @OperLog(operDesc = "CreateDealer", operModul = "Dealer", operType = "Create")
    @RequiresPermissions(PermissionsConst.DEALER_NEW)
    public ApiRestResult createDealer(@Valid @RequestBody SysDealerNewDto dto){
        SysUser currentUser = JwtUtil.getCurrentUser();
        SysDealer sysDealer = sysDealerService.findByDealerId(currentUser.getDealerId());
        dto.setRegistCode(sysDealer.getInviteCode());
        dto.setNewType(Constants.DEALER_STATE_ENABLE); //创建
        ApiRestResult result = sysDealerService.createDealer(dto);
        return result;
    }

    @PostMapping("/edit")
    @ApiOperation("修改经销商户")
    @OperLog(operDesc = "EditDealer", operModul = "Dealer", operType = "Edit")
    @RequiresPermissions(PermissionsConst.DEALER_EDIT)
    public ApiRestResult editDealer(@Valid @RequestBody SysDealerEditDto sysDealerEditDto){
        ApiRestResult result = sysDealerService.editDealer(sysDealerEditDto);
        return result;
    }

    @PostMapping("/editaddr")
    @ApiOperation("修改商户地址")
    @OperLog(operDesc = "EditAddr", operModul = "Dealer", operType = "EditAddr")
    @RequiresPermissions(PermissionsConst.DEALER_EDIT)
    public ApiRestResult editDealerAddr(@Valid @RequestBody SysAddrEditDto dto){
        sysAddrService.editAddr(dto);
        return ApiRestResult.ok();
    }


    @PostMapping("/admin_query_dealer")
    @ApiOperation("管理员查询待定经销商")
    @RequiresPermissions(PermissionsConst.DASHBOARD_ADMIN_QUERY)
    public ApiRestResult adminPendingDealerPage(@RequestBody SysDealerQueryDto dto){
        IPage<SysDealer> pageList = sysDealerService.queryPendingDealer(dto);
        return ApiRestResult.ok(pageList);
    }

    @PostMapping("/query")
    @ApiOperation("分页查询主经销商")
    @RequiresPermissions(PermissionsConst.DEALER_QUERY)
    public ApiRestResult masterDealerPage(@RequestBody SysDealerQueryDto dto){
        IPage<SysDealer> pageList = sysDealerService.masterDealerQuery(dto);
        return ApiRestResult.ok(pageList);
    }

    @PostMapping("/query_master")
    @ApiOperation("查询全部主经销商")
    @RequiresPermissions(PermissionsConst.DEALER_QUERY_ALL_MASTER)
    public ApiRestResult queryMasterDealer(){
        List<SysDealerRes> masterDealerList = sysDealerService.queryMasterDealer();
        return ApiRestResult.ok(masterDealerList);
    }

    @PostMapping("/query_dealer")
    @ApiOperation("查询主经销商下面的所有经销商")
    public ApiRestResult queryDealer(@RequestBody SysDealerQueryDto sysDealerQueryDto){
        IPage<SysDealer> pageList = sysDealerService.queryAllDealerById(sysDealerQueryDto);
        return ApiRestResult.ok(pageList);
    }

    @PostMapping("/dashboard_query_dealer")
    @ApiOperation("仪表盘查询所有待定经销商")
    public ApiRestResult queryPendingDealer(@RequestBody SysDealerQueryDto dto){
        IPage<SysDealer> pageList = sysDealerService.findAllPendingDealer(dto);
        return ApiRestResult.ok(pageList);
    }

    @PostMapping("/query_one")
    @ApiOperation("查询当前经销商")
    public ApiRestResult queryDealer(){
        SysDealer sysDealer = sysDealerService.findCurrentDealer();
        return ApiRestResult.ok(sysDealer);
    }

    @PostMapping("/querydealer")
    @ApiOperation("通过经销商id查询经销商")
    public ApiRestResult queryByDealerId(@RequestBody SysDealerDto dto){
        SysDealer sysDealer = sysDealerService.queryDealerByDealerId(dto);
        return ApiRestResult.ok(sysDealer);
    }

    @PostMapping("/delete")
    @ApiOperation("根据经销商id删除")
    @OperLog(operDesc = "DeleteDealer", operModul = "Dealer", operType = "Delete")
    public ApiRestResult deleteDealer(@RequestBody List<Long> ids){
        sysDealerService.batchDeleteDealer(ids);
        return ApiRestResult.ok();
    }

    /** 邮件中跳转
     *  经销商注册，点击邮箱认证按钮，跳转的接口
     */
    @Ignore
    @GetMapping("/verify/mail")
    @ApiOperation("电子邮箱认证")
    public ApiRestResult verifyMail(String token){
        sysDealerService.notifyMail(token);
        return ApiRestResult.ok();
    }

    /**
     * 主经销商认证经销商的申请
     */
    @PostMapping("/mdealer/verify")
    @ApiOperation("经销商申请注册，主经销商认证通过,后由持有者批准")
    public ApiRestResult verifyDealer(@RequestBody @Valid DealerStateEditDto dto){
        sysDealerService.verifyDealer(dto);
        return ApiRestResult.ok();
    }

    /**
     * 经销商申请注册之后，owner处理经销商状态
     * @return
     */
    @PostMapping("/owner/pass")
    @ApiOperation("持有者通过经销商的注册申请")
    public ApiRestResult passDealer(@RequestBody @Valid DealerStateEditDto dto){
        dto.setUpdateAt(new Date());
        dto.setState(Constants.DEALER_STATE_ENABLE);
        sysDealerService.updateStateById(dto);
        return ApiRestResult.ok();
    }
}
